<template>
  <j-modal title="查看领料单" :width="1200" :visible="visible" :footer="null" @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-descriptions title="基本信息" bordered :column="3">
        <a-descriptions-item label="领料单号">{{ model.requisitionNo }}</a-descriptions-item>
        <a-descriptions-item label="订单编号">{{ model.orderNo }}</a-descriptions-item>
        <a-descriptions-item label="优化类型">
          <a-tag :color="model.optimizationSoftwareType == '0' ? 'blue' : 'purple'">
            {{ model.optimizationSoftwareType == '0' ? '长风' : '新格尔' }}
          </a-tag>
        </a-descriptions-item>
        <a-descriptions-item label="审核状态">
          <a-tag :color="model.status == '0' ? 'orange' : model.status == '1' ? 'green' : 'red'">
            {{ model.status == '0' ? '待审核' : model.status == '1' ? '已审核' : '已驳回' }}
          </a-tag>
        </a-descriptions-item>
        <a-descriptions-item label="审核人">{{ model.approverName || '-' }}</a-descriptions-item>
        <a-descriptions-item label="审核时间">{{ model.approveTime || '-' }}</a-descriptions-item>
        <a-descriptions-item label="审核意见" :span="3" v-if="model.status != '0'">{{
          model.approveComment || '-'
        }}</a-descriptions-item>
        <a-descriptions-item label="备注" :span="3">{{ model.remark || '-' }}</a-descriptions-item>
      </a-descriptions>

      <!-- 型材明细表格 -->
      <div v-if="showProfileTable">
        <div style="margin-top: 20px; margin-bottom: 10px">
          <a-divider orientation="center">型材明细</a-divider>
        </div>

        <a-table
          ref="profileTable"
          size="middle"
          bordered
          rowKey="id"
          :columns="profileColumns"
          :dataSource="profileDataSource"
          :pagination="false"
        >
          <template slot="statusSlot" slot-scope="text">
            <a-tag :color="text == '0' ? 'orange' : text == '1' ? 'green' : 'blue'">
              {{ text == '0' ? '未领料' : text == '1' ? '已领料' : '部分领料' }}
            </a-tag>
          </template>
        </a-table>
      </div>

      <!-- 配件明细表格 -->
      <div v-if="showAccessoryTable">
        <div style="margin-top: 20px; margin-bottom: 10px">
          <a-divider orientation="center">配件明细</a-divider>
        </div>

        <a-table
          ref="accessoryTable"
          size="middle"
          bordered
          rowKey="id"
          :columns="accessoryColumns"
          :dataSource="accessoryDataSource"
          :pagination="false"
        >
          <template slot="statusSlot" slot-scope="text">
            <a-tag :color="text == '0' ? 'orange' : text == '1' ? 'green' : 'blue'">
              {{ text == '0' ? '未领料' : text == '1' ? '已领料' : '部分领料' }}
            </a-tag>
          </template>
        </a-table>
      </div>
    </a-spin>
  </j-modal>
</template>

<script>
import { getAction } from '@/api/manage'

export default {
  name: 'RequisitionViewModal',
  data() {
    return {
      title: '查看领料单',
      visible: false,
      model: {},
      confirmLoading: false,

      // 型材明细相关
      profileDataSource: [],
      showProfileTable: true,
      profileColumns: [
        { title: '型材编号', dataIndex: 'profileCode', align: 'center' },
        { title: '型材名称', dataIndex: 'profileName', align: 'center' },
        { title: '颜色', dataIndex: 'color', align: 'center' },
        { title: '长度(mm)', dataIndex: 'length', align: 'center' },
        { title: '宽度(mm)', dataIndex: 'width', align: 'center' },
        { title: '高度(mm)', dataIndex: 'height', align: 'center' },
        { title: '数量(根)', dataIndex: 'quantity', align: 'center' },
        { title: '重量(kg)', dataIndex: 'weight', align: 'center' },
        { title: '备注', dataIndex: 'remark', align: 'center' },
        { title: '领料状态', dataIndex: 'status', align: 'center', scopedSlots: { customRender: 'statusSlot' } },
      ],

      // 配件明细相关
      accessoryDataSource: [],
      showAccessoryTable: true,
      accessoryColumns: [
        { title: '配件编号', dataIndex: 'accessoryCode', align: 'center' },
        { title: '配件名称', dataIndex: 'accessoryName', align: 'center' },
        { title: '规格', dataIndex: 'specification', align: 'center' },
        { title: '数量', dataIndex: 'quantity', align: 'center' },
        { title: '长度/数量', dataIndex: 'lengthQuantity', align: 'center' },
        { title: '单位', dataIndex: 'unit', align: 'center' },
        { title: '品牌', dataIndex: 'brand', align: 'center' },
        { title: '领料状态', dataIndex: 'status', align: 'center', scopedSlots: { customRender: 'statusSlot' } },
      ],

      url: {
        getRequisition: '/wms/requisition/queryById',
        getProfileList: '/wms/requisition/getProfileList',
        getAccessoryList: '/wms/requisition/getAccessoryList',
      },
    }
  },
  methods: {
    show(id) {
      this.visible = true
      this.loadData(id)
    },

    loadData(id) {
      this.confirmLoading = true

      // 清空数据
      this.model = {}
      this.profileDataSource = []
      this.accessoryDataSource = []

      // 加载领料单数据
      getAction(`${this.url.getRequisition}?id=${id}`)
        .then((res) => {
          if (res.success) {
            this.model = res.result || {}

            // 根据优化软件类型调整表格显示和列
            this.adjustTablesByType(this.model.optimizationSoftwareType)

            // 加载型材明细
            this.loadProfileData(id)

            // 加载配件明细
            this.loadAccessoryData(id)
          }
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },

    // 加载型材明细数据
    loadProfileData(requisitionId) {
      getAction(`${this.url.getProfileList}/${requisitionId}`).then((res) => {
        if (res.success) {
          this.profileDataSource = res.result || []
        }
      })
    },

    // 加载配件明细数据
    loadAccessoryData(requisitionId) {
      getAction(`${this.url.getAccessoryList}/${requisitionId}`).then((res) => {
        if (res.success) {
          this.accessoryDataSource = res.result || []
        }
      })
    },

    // 根据优化软件类型调整表格
    adjustTablesByType(type) {
      if (type == '0') {
        // 长风
        this.showProfileTable = true
        this.showAccessoryTable = true

        // 调整型材列
        this.profileColumns = [
          { title: '型材编号', dataIndex: 'profileCode', align: 'center' },
          { title: '型材名称', dataIndex: 'profileName', align: 'center' },
          { title: '颜色', dataIndex: 'color', align: 'center' },
          { title: '长度(mm)', dataIndex: 'length', align: 'center' },
          { title: '宽度(mm)', dataIndex: 'width', align: 'center' },
          { title: '高度(mm)', dataIndex: 'height', align: 'center' },
          { title: '数量(根)', dataIndex: 'quantity', align: 'center' },
          { title: '重量(kg)', dataIndex: 'weight', align: 'center' },
          { title: '备注', dataIndex: 'remark', align: 'center' },
          { title: '领料状态', dataIndex: 'status', align: 'center', scopedSlots: { customRender: 'statusSlot' } },
        ]

        // 调整配件列
        this.accessoryColumns = [
          { title: '配件编号', dataIndex: 'accessoryCode', align: 'center' },
          { title: '配件名称', dataIndex: 'accessoryName', align: 'center' },
          { title: '规格', dataIndex: 'specification', align: 'center' },
          { title: '数量', dataIndex: 'quantity', align: 'center' },
          { title: '单位', dataIndex: 'unit', align: 'center' },
          { title: '品牌', dataIndex: 'brand', align: 'center' },
          { title: '领料状态', dataIndex: 'status', align: 'center', scopedSlots: { customRender: 'statusSlot' } },
        ]
      } else if (type == '1') {
        // 新格尔
        this.showProfileTable = true
        this.showAccessoryTable = true

        // 调整型材列 (新格尔可能需要不同的列)
        this.profileColumns = [
          { title: '型材编号', dataIndex: 'profileCode', align: 'center' },
          { title: '型材名称', dataIndex: 'profileName', align: 'center' },
          { title: '颜色', dataIndex: 'color', align: 'center' },
          { title: '数量(根)', dataIndex: 'quantity', align: 'center' },
          { title: '长度(mm)', dataIndex: 'length', align: 'center' },
          { title: '重量(kg)', dataIndex: 'weight', align: 'center' },
          { title: '备注', dataIndex: 'remark', align: 'center' },
          { title: '领料状态', dataIndex: 'status', align: 'center', scopedSlots: { customRender: 'statusSlot' } },
        ]

        // 调整配件列 (新格尔可能需要不同的列)
        this.accessoryColumns = [
          { title: '配件编号', dataIndex: 'accessoryCode', align: 'center' },
          { title: '配件名称', dataIndex: 'accessoryName', align: 'center' },
          { title: '长度/数量', dataIndex: 'lengthQuantity', align: 'center' },
          { title: '单位', dataIndex: 'unit', align: 'center' },
          { title: '领料状态', dataIndex: 'status', align: 'center', scopedSlots: { customRender: 'statusSlot' } },
        ]
      }
    },

    handleCancel() {
      this.visible = false
    },
  },
}
</script>

<style scoped>
</style> 